Skip to content

Conversation

nrcrast
Copy link

@nrcrast nrcrast commented Jun 17, 2025

Fix #59

This updates the tsconfig and package.json (and all the other consequences of that) to export proper ESM modules.

You can test this pretty easily with a project like the following:

package.json

{
  "name": "json-validator-test",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "start": "node src/index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "json-schema-library": "10.5.2",
    "tsx": "^4.19.3"
  },
  "devDependencies": {
    "@types/node": "^24.0.3",
    "typescript": "^5.8.3"
  }
}

src/index.js

import {compileSchema} from "json-schema-library";

const schema = compileSchema({
  "$id": "https://example.com/person.schema.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Person",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string",
      "description": "The person's first name."
    }
  }
});

console.log('Hello world', schema);

Using plain ole npm with node v22, running npm i && npm run start you'll get something like this:

file:///Users/206674672/code/json-validator-test/src/index.js:1
import {compileSchema} from "json-schema-library";
        ^^^^^^^^^^^^^
SyntaxError: Named export 'compileSchema' not found. The requested module 'json-schema-library' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'json-schema-library';
const {compileSchema} = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:177:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:260:5)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:543:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:116:5)

Replacing the version with my branch: :

    "json-schema-library": "[email protected]:nrcrast/json-schema-library.git#66bb39f1b68d518ee67707d88ea0598dec7e8a51",

You'll get the expected output where it dumps the whole compiled schema:

> [email protected] start
> node src/index.js

Hello world <ref *1> {
  evaluationPath: '#',
  lastIdPointer: '#',
  schemaLocation: '#',
  dynamicId: '',
....etc

@sagold
Copy link
Owner

sagold commented Jul 2, 2025

Hi Nick,

thank you for your PR!! I will need some time to verify your changes and test other types of exports. I have a setup
prepared to build an esm module without those changes, but your PR might be a better way forward.

Cheers,
Sascha

@navnita123
Copy link

Please release this, we need modern esm support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Doesn't work with native ESM modules
3 participants